package com.chukun.netty.codec;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.UUID;

/**
 * @author chukun
 * 服务端编解码器
 */
public class NettyCodecServerHandler extends SimpleChannelInboundHandler<String> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String s) throws Exception {
        System.out.println("received client :"+ctx.channel().remoteAddress()+" : "+s);
        ctx.channel().writeAndFlush("send to "+ctx.channel().remoteAddress()+"-->"+UUID.randomUUID().toString());
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}
